/*
 * Player.cpp
 *
 *  Created on: Jul 13, 2009
 *      Author: scott
 */
#include <allegro.h>

#include "Player.h"
#include "World.h"
#include "MeshObject.h"
#include "Dust.h"
#include "Blower.h"

Player::Player(World * world, SceneGraphNode * parent, bool ownsChildren)
	: ObjectNode(parent, DRAW_MIDDLE, NULL, -1, ownsChildren)
{
	// generate a body
	body = dBodyCreate(world->getWorld());
	dBodySetData(body, (void*) NULL);
	dMass m;
	dMassSetSphere(&m, 1, 2);
	dMassAdjust(&m, 1);
	dBodySetMass(body,&m);
	dGeomID sphere = dCreateSphere(0,0.01);
	dGeomSetBody(sphere, body);
	dBodySetPosition(body, 0, 0, 0);
	irr::core::matrix4 rot;
	rot.setRotationDegrees(getRotation());
	float rotBuffer[16];
	dBodySetRotation(body,(const dReal*) World::opengl2OdeR(rotBuffer, rot.pointer()));
	dSpaceAdd(world->getSpace(), sphere);

	for (int i = 0; i < 10; i++)
	{
		weapons[i] = 0;
	}

	weapon = 0;
}

Player::~Player() {

}

void Player::update()
{
	irr::core::vector3df pos = parent->getTranslation();
	dBodySetPosition(body, pos.X, pos.Y, pos.Z);

	if ((mouse_b & 1) && weapon)
	{
		weapon->use();
	}

	for (int keyIndex = KEY_0; keyIndex <= KEY_9; keyIndex++)
	{
		if (key[keyIndex] && weapons[keyIndex - KEY_0])
		{
			switchTo(keyIndex - KEY_0);
		}
	}
}

void Player::switchTo(int weaponNum)
{
	if (weapon)
	{
		weapon->setVisible(false);
		weapon->switchFrom();
	}
	weapon = weapons[weaponNum];
	weapon->switchTo();
	weapon->setVisible(true);
}

void Player::addWeapon(Weapon *weapon, int weaponNum)
{
	weapon->setVisible(false);
	weapons[weaponNum] = weapon;
}


